//
//  W_DealTableViewCell.m
//  Go!
//
//  Created by 凌風 on 16/7/13.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "W_DealTableViewCell.h"
#import "W_DealModel.h"
#import "UIImageView+ImageWithURL.h"

@interface W_DealTableViewCell ()

@property (nonatomic, strong) UIImageView *myImageView;
@property (nonatomic, strong) UILabel_Font *titleLabel;
@property (nonatomic, strong) UILabel_Font *marketPriceLabel;
@property (nonatomic, strong) UILabel_Font *promotionPriceLabel;
@property (nonatomic, strong) UIButton *detailBtn;
@property (nonatomic, strong) UILabel_Font *detailLabel;
@property (nonatomic, strong) UIView *separatorLine;
@property (nonatomic, strong) UILabel_Font *saleNumLabel;
@end

@implementation W_DealTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    [self addSubview:self.myImageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.marketPriceLabel];
    [self addSubview:self.promotionPriceLabel];
    [self addSubview:self.detailBtn];
    [self addSubview:self.detailLabel];
    [self addSubview:self.saleNumLabel];
    [self addSubview:self.separatorLine];
    [self buildFrame];
}

//- (void)layoutSubviews {
-(void)buildFrame{
    self.myImageView.frame = CGRectMake(Size_W(8), Size_H(8), Size_W(80), Size_H(80));
    self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.myImageView.frame) + Size_W(8), CGRectGetMinY(self.myImageView.frame), ScreenW - CGRectGetMaxX(self.myImageView.frame) - Size_W(8) - Size_W(56), Size_H(30));
    [self.titleLabel sizeToFit];
    
    self.marketPriceLabel.frame = CGRectMake(CGRectGetMinX(self.titleLabel.frame), CGRectGetMidY(self.myImageView.frame) - Size_H(30) / 2, CGRectGetWidth(self.titleLabel.bounds), Size_H(30));
    [self.marketPriceLabel sizeToFit];
    
    self.promotionPriceLabel.frame = CGRectMake(CGRectGetMinX(self.titleLabel.frame), CGRectGetMaxY(self.myImageView.frame) - Size_H(30), CGRectGetWidth(self.titleLabel.bounds), Size_H(30));
    [self.promotionPriceLabel sizeToFit];
    
    self.detailBtn.frame = CGRectMake(ScreenW - Size_W(53), CGRectGetMidY(self.myImageView.frame) - Size_H(40) / 2 - Size_H(5), Size_W(40), Size_H(40));
    
    self.saleNumLabel.bounds = CGRectMake(0, 0, Size_W(50), Size_H(30));
    self.saleNumLabel.center = CGPointMake(CGRectGetMidX(self.detailBtn.frame) - Size_W(10), CGRectGetMaxY(self.detailBtn.frame) + CGRectGetMidY(self.saleNumLabel.bounds) + Size_H(8));
    [self.saleNumLabel sizeToFit];
    
    self.detailLabel.frame = CGRectMake(Size_W(8), CGRectGetMaxY(self.myImageView.frame) + Size_H(2), ScreenW - Size_W(64), Size_H(40));
    [self.detailLabel sizeToFit];
    
    self.separatorLine.frame = CGRectMake(CGRectGetMinX(self.myImageView.frame), CGRectGetMaxY(self.detailLabel.frame) - 0.5, ScreenW - CGRectGetMinX(self.myImageView.frame), 1);
    
}



#pragma mark -- 监听事件的回调方法
- (void)respondsToDetailBtn:(UIButton *)sender {
    
}

#pragma mark -- Setter
-(void)setModel:(W_DealModel *)model {
    if (_model != model) {
        _model = model;
    }
    
    [self.myImageView setImageWithURL:model.imageURL placeHolderImage:[UIImage imageNamed:@"景点郊游"]];
    self.titleLabel.text = model.deal_name;
    self.marketPriceLabel.text = model.marketPrice;
    self.promotionPriceLabel.text = model.promotionPrice;
    self.detailLabel.text = model.deal_description;
    self.saleNumLabel.text = model.sale_num;
    
    [self buildFrame];
}

#pragma mark -- Getter
- (UIImageView *)myImageView {
    if (!_myImageView) {
        _myImageView = [[UIImageView alloc] init];
//        _myImageView.image = [UIImage imageNamed:@"景点郊游"];
    }
    
    return _myImageView;
}

- (UILabel_Font *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel_Font alloc] init];
        _titleLabel.text = @"旅游";
        _titleLabel.font = [UIFont boldSystemFontOfSize:Font(20)];
        
    }
    
    return _titleLabel;
}

- (UILabel_Font *)marketPriceLabel {
    if (!_marketPriceLabel) {
        _marketPriceLabel = [[UILabel_Font alloc] init];
        _marketPriceLabel.text = @"市场价:￥100";
        _marketPriceLabel.textColor = [UIColor greenColor];
        
    }
    
    return _marketPriceLabel;
}

- (UILabel_Font *)promotionPriceLabel {
    if (!_promotionPriceLabel) {
        _promotionPriceLabel = [[UILabel_Font alloc] init];
        _promotionPriceLabel.text = @"促销价:￥80";
        _promotionPriceLabel.textColor = [UIColor redColor];

    }
    
    return _promotionPriceLabel;
}

- (UIButton *)detailBtn {
    if (!_detailBtn) {
        _detailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_detailBtn setImage:[UIImage imageNamed:@"详情.png"] forState:UIControlStateNormal];
        
        [_detailBtn addTarget:self action:@selector(respondsToDetailBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _detailBtn;
}

- (UILabel_Font *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel_Font alloc] init];
        _detailLabel.text = @"简介";
        _detailLabel.textColor = [UIColor grayColor];
        _detailLabel.font = [UIFont systemFontOfSize:Font(17)];
        
        _detailLabel.numberOfLines = 0;
    }
    
    return _detailLabel;
}

- (UILabel_Font *)saleNumLabel {
    if (!_saleNumLabel) {
        _saleNumLabel = [[UILabel_Font alloc] init];
        _saleNumLabel.textColor = [UIColor grayColor];
        _saleNumLabel.font = [UIFont systemFontOfSize:Font(14)];
//        _saleNumLabel.text = @"已售出13";
    }
    
    return _saleNumLabel;
}


- (UIView *)separatorLine {
    if (!_separatorLine) {
        _separatorLine = [[UIView alloc] init];
        _separatorLine.backgroundColor = [UIColor grayColor];
    }
    
    return _separatorLine;
}

@end
